﻿namespace Sirius.Log
{
    public  delegate IntPtr abcd (string a);
    public class Log
    {
        private static Object logLock = new Object ();
        //输出普通信息
        public static void LogOut(String role, String msg)
        {
            lock (logLock)
            {
                Console.ForegroundColor = ConsoleColor.White;//设置成白色
                Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + " " + role + "-> INFO]：" + msg);
            }

        }
        //输出正确信息
        public static void LogSuc(String role, String msg)
        {
            lock (logLock)
            {
                Console.ForegroundColor = ConsoleColor.Green;//设置绿色
                Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + " " + role + "-> OK]：" + msg);
                Console.ForegroundColor = ConsoleColor.White;//恢复白色
            }
        }

        //输出警告信息
        public static void LogWar(String role, String msg)
        {
            lock (logLock)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;//设置成黄色
                Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + " " + role + "-> WARN]：" + msg);
                Console.ForegroundColor = ConsoleColor.White;//恢复白色
            }
        }
        //输出错误信息
        public static void LogErr(String role, String msg)
        {
            lock (logLock)
            {
                Console.ForegroundColor = ConsoleColor.Red;//设置为红色
                Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + " " + role + "-> ERROR]：" + msg);
                Console.ForegroundColor = ConsoleColor.White;//恢复白色
            }
        }
        //输出错误信息
        public static void LogErr(String role, Exception e)
        {
            lock (logLock)
            {
                Console.ForegroundColor = ConsoleColor.Red;//设置为红色
                Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + " " + role + "-> ERROR]：" + e);
                Console.ForegroundColor = ConsoleColor.White;//恢复白色
            }
        }

        //OpenApi调用日志
        public static void LogOpenApi(string ApiName,bool result,Exception? exception,string response)
        {
            lock (logLock)
            {
                var date = DateTime.Now;
                var year = date.Year;
                var month = date.Month;
                var day = date.Day;
                try
                {
                    if (result)
                    {
                        Console.ForegroundColor = ConsoleColor.Blue;//设置为蓝色
                        Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + " " + ApiName + "-> ApiLog]：" + "Api调用成功！");
                        Utils.Util.logWriter(year + "-" + month + "-" + day + ".log", response);
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.DarkRed;//设置为灰色
                        Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + " " + ApiName + "-> ApiLog]：" + "Api调用异常！" + exception.ToString());
                        Utils.Util.logWriter(year + "-" + month + "-" + day + ".log", response);
                    }
                    Console.ForegroundColor = ConsoleColor.White;//恢复白色
                    System.GC.Collect();
                    Console.WriteLine("已释放内存");
                }
                catch (Exception ex)
                {
                    LogErr("LogOpenApi", ex);
                }
            }
        }

        //事件消息输出
        public static void LogEvent(string eventName, string message)
        {
            lock (logLock)
            {
                Console.BackgroundColor = ConsoleColor.Red;//设置背景为红色
                Console.ForegroundColor = ConsoleColor.Yellow;//设置文字颜色为黄色
                Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + " " + eventName + "-> eventLog]：" + message);
                Console.BackgroundColor = ConsoleColor.Black;//恢复背景为黑色
                Console.ForegroundColor = ConsoleColor.White;//恢复文字颜色为白色
            }
        }

    }
}
